/**
 * 
 */
package com.jm.web.form;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspWriter;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.form.support.FormItem;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 * Nov 23, 2011 9:50:25 AM
 */
public class TableToolbar extends FormItem{
	protected List defaultItems = null ;
	private Button addRowButton = null;
	private Button saveRowButton = null;
	private Button deleteRowButton = null;
	
	

	private boolean bLeft = true;
	/**
	 * 
	 */
	public TableToolbar() {
		addRowButton = Toolbar.getNewButton("新增行","addrow","addrow","xt-btn-icon sx-tb-addrow",null);
		saveRowButton = Toolbar.getNewButton("保存行","saverow","saverow","xt-btn-icon sx-tb-saverow",null);
		deleteRowButton = Toolbar.getNewButton("刪除行","deleterow","deleterow","xt-btn-icon sx-tb-delete",null);
		deleteRowButton.setEnab(false);
		saveRowButton.setEnab(false);
	}
	
	@Override
	public void InitTag(BaseTag tag) {
		super.InitTag(tag);
		if(this.getBaseTag() != null){
			this.setId(this.getBaseTag().GetExtValue("id",""));
			this.SetExtValue("deleterowconfig",this.getBaseTag().GetExtValue("deleterowconfig","arg:null"));
			this.SetExtValue("addrowconfig",this.getBaseTag().GetExtValue("addrowconfig","arg:null"));
		}
	}
	
	@Override
	public void init() {
		LoadButtons();
		//处理自定义的工具栏按钮
		List customItems = this.getItems();
		if(customItems != null)
		{
			for (Iterator iter = customItems.iterator(); iter.hasNext();) {
				FormItem item = (FormItem) iter.next();
				item.SetExtValue("extAtr", " tid=\""+this.getId()+"\" ");
				if(item.getPos() > 0 && item.getPos() < defaultItems.size()){
					defaultItems.add(item.getPos(),item);
				}else{
					defaultItems.add(item);
				}
			}
			
			this.getItems().clear();
			this.getItems().addAll(defaultItems);
		}
	}
	
	@Override
	public void outItemBegin(JspWriter writer) {
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("<table class='tabletoolbar' style=\"overflow: hidden;\" id=\"tabletoolbar-%1$s\" tid=\"%1$s\" width=\"100%%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" height=\"23\" ><tbody><tr>",this.getId());
		//sb.Append("<table class='tabletoolbar' style=\"overflow: hidden;\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" height=\"23\" ><tbody><tr>");
		try {
			writer.write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void outItemBody(JspWriter writer) {
		try {
			for (Iterator iterator = this.items.iterator(); iterator.hasNext();) {
				Object object = iterator.next();
				if(object instanceof Separate){
					Separate sep = (Separate)object;
					writer.write("\r\n<td style=\"border:0px !important;\" width=\"2px\">");
					
					sep.onRender(writer);
					writer.write("</td>");
				}else if(object instanceof Button){
					Button button = (Button)object;
					if(button.isEnab()){
						writer.write(StringHelper.Format("\r\n<td class=\"toobar-td-cell\" width=\"%1$s\" style=\"border:0px !important; width:%1$s; \" >",button.getWidth()));
						button.onRender(writer);
						//writer.write(button.toButtonString());
						writer.write("</td>");
					}
				}else if(object instanceof FormItem){
						FormItem item = (FormItem)object;
						writer.write(StringHelper.Format("\r\n<td class=\"toobar-td-cell\" width=\"%1$s\"  style=\"border:0px !important; width:%1$s;\" >",item.getWidth()));
						item.onRender(writer);
						writer.write("</td>");
				}else if(object instanceof Empty){
					Empty item = (Empty)object;
					writer.write(StringHelper.Format("\r\n<td class=\"toobar-td-cell\" width=\"%1$s\"  style=\"border:0px !important; width:%1$s;\" >",item.getWidth()));
					writer.write("&nbsp;");
					writer.write("</td>");
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void outItemEnd(JspWriter writer) {
		StringBuilderEx sb = new StringBuilderEx();
		if(this.isLeft())
			sb.Append("<td width=\"100%\" style=\"border:0px !important;\">&nbsp;</td>");
		sb.Append("</tr><tbody></table>");
		try {
			writer.write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 加载默认的工具栏
	 * @throws Exception 
	 */
	protected void LoadButtons() {
		if(defaultItems == null)
			defaultItems = new ArrayList();
		else
			defaultItems.clear();
		
		Separate sep = new Separate();
		sep.InitTag(this.getBaseTag());
		
		
		if(addRowButton.isEnab()){
//			addRowButton.SetExtValue("extInfo","onmousedown=\"if($(this).isactive())$(this).addClass('ax-btn-pressed');\" onmouseup=\"if($(this).isactive())$(this).removeClass('ax-btn-pressed');\"  onmouseover=\"if($(this).isactive())$(this).addClass('ax-btn-over');\" onmouseout=\"if($(this).isactive())$(this).removeClass('ax-btn-over').removeClass('ax-btn-pressed');\"");
			if(addRowButton.isNullClick())
				addRowButton.setOnClick(StringHelper.Format("$P.grid.addrow({tid:'%1$s',%2$s});",
						this.getId(),this.GetExtValue("addrowconfig","arg:null")));
			
			defaultItems.add(addRowButton);
		}
		
		if(defaultItems.size() > 0)
			defaultItems.add(sep);
		
		if(deleteRowButton.isEnab()){
//			deleteRowButton.SetExtValue("extInfo","onmousedown=\"if($(this).isactive())$(this).addClass('ax-btn-pressed');\" onmouseup=\"if($(this).isactive())$(this).removeClass('ax-btn-pressed');\"  onmouseover=\"if($(this).isactive())$(this).addClass('ax-btn-over');\" onmouseout=\"if($(this).isactive())$(this).removeClass('ax-btn-over').removeClass('ax-btn-pressed');\"");
			if(deleteRowButton.isNullClick())
				deleteRowButton.setOnClick(StringHelper.Format("$P.grid.deleterow({tid:'%1$s',%2$s});",
						this.getId(),this.GetExtValue("deleterowconfig","arg:null")));
			defaultItems.add(deleteRowButton);
		}
		
		if(saveRowButton.isEnab()){
//			saveRowButton.SetExtValue("extInfo","onmousedown=\"if($(this).isactive())$(this).addClass('ax-btn-pressed');\" onmouseup=\"if($(this).isactive())$(this).removeClass('ax-btn-pressed');\"  onmouseover=\"if($(this).isactive())$(this).addClass('ax-btn-over');\" onmouseout=\"if($(this).isactive())$(this).removeClass('ax-btn-over').removeClass('ax-btn-pressed');\"");
			defaultItems.add(saveRowButton);
		}
	}

	public Button getSaveRowButton() {
		return saveRowButton;
	}

	public void setSaveRowButton(Button saveRowButton) {
		this.saveRowButton = saveRowButton;
	}
	
	public Button getAddRowButton() {
		return addRowButton;
	}

	public void setAddRowButton(Button addRowButton) {
		this.addRowButton = addRowButton;
	}
	
	public Button getDeleteRowButton() {
		return deleteRowButton;
	}

	public void setDeleteRowButton(Button deleteRowButton) {
		this.deleteRowButton = deleteRowButton;
	}
	
	public boolean isLeft() {
		return bLeft;
	}

	public void setLeft(boolean left) {
		bLeft = left;
	}
}
